package mydatisdb.mapper;

import mydatisdb.pojo.Product;
import mydatisdb.util.DBUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.PreparedStatement;
@Component

public class ProductMapper implements IProductMapper{
    @Autowired
    private DBUtil dbUtil;
    @Override

    public int insertProduct(Product product) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql = "insert into product(title,price,stock) values(?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, product.getTitle());
            ps.setInt(2, product.getPrice());
            ps.setInt(3, product.getStock());
            int rs = ps.executeUpdate();
            return rs;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;

    }
}
